package org.example.news.controller;

import org.example.news.pojo.Result;
import org.example.news.utils.AliOssUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController
public class FileUpdateController {

    @PostMapping("/upload")
    public Result<String> upload(MultipartFile file) throws Exception {


        //把文件的内容存储到本地磁盘上
        String originalFilename=file.getOriginalFilename();   //获取文件原有的名字
        //确保文件的名字是唯一的的，从而防止覆盖
        String filename= UUID.randomUUID().toString()+originalFilename.substring(originalFilename.lastIndexOf("."));
        //file.transferTo(new File("F:\\Files\\"+filename));  //存储到本地磁盘上
      String   url= AliOssUtil.uploadFile(filename,file.getInputStream());
        return Result.success(url);

    }
}
